<template>
	<view class="address-edit">
		<view class="form bg-white">
			<u-field v-model="addressObj.contact" label="收货人" placeholder="请填写收货人姓名">
			</u-field>
			<u-field v-model="addressObj.telephone" label="联系方式" placeholder="请填写手机号码">
			</u-field>
			<!-- <view @click="showRegion = true"> -->
			<view @click="openMap">
				<u-field v-if="addressObj.address_position.length==0 || addressObj.address_name.length==0" :disabled="true"
					label="选择地址" placeholder="请选择地址" right-icon="arrow-right">
				</u-field>
				<view class="inpuBox" v-else>
					<view class="inputLabel"> 选择地址 </view>
					<view class="box1">
						<view class="inpu1"> {{addressObj.address_name}} </view>
						<view class="inpu2"> {{addressObj.address_position}} </view>
					</view>
					<view class="inputBtn"><u-icon name="arrow-right" color="#c0c4cc" size="26"></u-icon></view>
				</view>
			</view>
			<view>
				<u-field v-model="addressObj.house_number" type="textarea" label="门牌号" placeholder="请填写详细地址如小区、街道、门牌号等信息"
					:field-style="{ flex: 1, minHeight: '200rpx' }" />
			</view>
		</view>
		<view class="m-t-10 m-b-10 bg-white p-20">
			<u-checkbox @click="changeDefault" v-model="addressObj.is_default" shape="circle">
				<text class="xs">设置为默认</text>
			</u-checkbox>
		</view>
		<button class="my-btn bg-primary white br60" @tap="formSubmit">完成</button>
		<!-- <u-select v-model="showRegion" mode="mutil-column-auto" @confirm="regionChange" :list="lists"></u-select> -->
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
	// | likeshop开源商城系统
	// +----------------------------------------------------------------------
	// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
	// | gitee下载：https://gitee.com/likeshop_gitee
	// | github下载：https://github.com/likeshop-github
	// | 访问官网：https://www.likeshop.cn
	// | 访问社区：https://home.likeshop.cn
	// | 访问手册：http://doc.likeshop.cn
	// | 微信公众号：likeshop技术社区
	// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
	// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
	// | 禁止对系统程序代码以任何目的，任何形式的再发布
	// | likeshop团队版权所有并拥有最终解释权
	// +----------------------------------------------------------------------
	// | author: likeshop.cn.team
	// +----------------------------------------------------------------------
	import {
		editAddress,
		getOneAddress,
		hasRegionCode,
		addAddress,
	} from "@/api/user";
	import {
		mapGetters
	} from "vuex";
	import area from "@/utils/area";
	export default {
		data() {
			return {
				addressObj: {
					contact: "",
					telephone: "",
					province: "",
					city: "",
					district: "",
					address: "",
					is_default: false,
					pname: "", //市名
					cityname: "", //城市名
					adname: "", //城区名
					address_position: '',
					address_name: '',
					house_number: '',
					latitude: 0,
					longitude: 0
				},
				addressId: "",
				defaultRegion: ["广东省", "广州市", "番禺区"],
				defaultRegionCode: "440113",
				showRegion: false,
				lists: [],
				choseAddressList: {}
			};
		},
		onLoad: function(options) {
			this.addressId = parseInt(options.id);
			if (options.id) {
				uni.setNavigationBarTitle({
					title: "编辑地址",
				});
				this.getOneAddressFun();
			} else {
				uni.setNavigationBarTitle({
					title: "添加地址",
				});
				this.getWxAddressFun();
			}
			this.$nextTick(() => {
				this.lists = area;
			});
		},
		onShow() {
			if (!this.choseAddressList.latitude) {
				// console.log('none!!!')
				return false
			} else {
				// this.addressObj = this.choseAddressList
				// console.log('接受地址yuan', this.choseAddressList)
				this.addressObj.pname = this.choseAddressList.pname; //市名
				this.addressObj.cityname = this.choseAddressList.cityname; //城市名
				this.addressObj.adname = this.choseAddressList.adname; //城区名
				this.addressObj.district = this.choseAddressList.district; //市区
				this.addressObj.address_position = this.choseAddressList.address;
				this.addressObj.address_name = this.choseAddressList.name;
				// this.addressObj.house_number = this.choseAddressList.name; 
				this.addressObj.latitude = this.choseAddressList.latitude;
				this.addressObj.longitude = this.choseAddressList.longitude;
				this.addressObj.address = '';
				// console.log('接收地址', this.addressObj)
			}

		},

		onUnload: function() {
			uni.removeStorageSync("wxAddress");
		},

		methods: {
			...mapGetters(["cityInfo"]),
			// 打开地图
			openMap() {
				let lng = this.cityInfo.gcj02_lng || ''
				let lat = this.cityInfo.gcj02_lat || ''
				uni.navigateTo({
					url: "bundle/pages/gaode-map/gaode-map?lng=" + lng + "&lat=" + lat
				})
			},
			async formSubmit() {
				let {
					addressObj: {
						contact,
						telephone,
						province_id,
						city_id,
						district_id,
						pname, //市名
						cityname, //城市名
						adname, //城区名
						address_position,
						address_name,
						house_number,
						is_default,
						latitude,
						longitude
					},
					addressId,
					// region,
				} = this;
				if (!contact)
					return this.$toast({
						title: "请填写收货人姓名",
					});
				if (!telephone)
					return this.$toast({
						title: "请填写手机号码",
					});
				if (!address_position || !address_name)
					return this.$toast({
						title: "请选择地址",
					});
				if (!house_number)
					return this.$toast({
						title: "请填写小区、街道、门牌号等信息",
					});
				const params = {
					contact,
					telephone,
					province_id: parseInt(province_id),
					city_id: parseInt(city_id),
					district_id: parseInt(district_id),
					pname, //市名
					cityname, //城市名
					adname, //城区名
					address_position,
					address_name,
					house_number,
					is_default: is_default ? 1 : 0,
					id: addressId,
					address: address_name + house_number,
					latitude,
					longitude
				};
				// console.log('params', params)
				const {
					code,
					msg
				} = addressId
					?
					await editAddress(params) :
					await addAddress(params);
				if (code == 1) {
					this.$toast({
						title: msg,
					}, {
						tab: 3,
						url: 2,
					});
				}
			},
			// regionChange(region) {
			// 	this.addressObj.province_id = region[0].value;
			// 	this.addressObj.city_id = region[1].value;
			// 	this.addressObj.district_id = region[2].value;
			// 	this.region =
			// 		region[0].label + " " + region[1].label + " " + region[2].label;
			// },

			getOneAddressFun() {
				getOneAddress(this.addressId).then((res) => {
					if (res.code == 1) {
						let {
							city,
							province,
							district
						} = res.data;
						this.addressObj = res.data;
						// console.log('编辑地址', this.addressObj)
						// this.addressObj.name = this.addressObj
						// this.region = `${province} ${city} ${district}`;
					}
				});
			},

			getWxAddressFun() {
				let wxAddress = uni.getStorageSync("wxAddress");
				if (!wxAddress) return;
				wxAddress = JSON.parse(wxAddress);
				let {
					userName: contact,
					telNumber: telephone,
					provinceName: province,
					cityName: city,
					detailInfo: address,
				} = wxAddress;
				let district = wxAddress.countryName || wxAddress.countyName;
				hasRegionCode({
					province,
					city,
					district,
				}).then((res) => {
					if (res.code == 1) {
						if (res.data.province && res.data.city && res.data.district) {
							// this.region = `${province} ${city} ${district}`;
							this.addressObj.province_id = res.data.province;
							this.addressObj.city_id = res.data.city;
							this.addressObj.district_id = res.data.district;
						}
						this.addressObj.contact = contact;
						this.addressObj.telephone = telephone;
						this.addressObj.address_position = address;
						this.addressObj.address_name = '--';

						// console.log('微信地址', res.data)
					}
				});
			},
		},
	};
</script>
<style lang="scss" scoped>
	.inpuBox {
		padding: 20rpx 28rpx;
		border-bottom: 1px solid #f6f6f6;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.inputLabel {
			width: 130rpx;
			text-align: left;
		}

		.box1 {
			flex: 1;
		}

		.inputBtn {}

		.inpu1 {
			font-size: 28rpx;
			font-weight: bold;
		}

		.inpu2 {
			font-size: 24rpx;
		}
	}

	.form {
		/deep/ .u-textarea-class {
			min-height: 60rpx;
		}
	}

	.address-edit {
		padding-top: 10rpx;

		.my-btn {
			margin: 30rpx 26rpx;
			text-align: center;
		}
	}
</style>